package com.dglg.spring.controller;

import com.dglg.spring.model.Scores;
import com.dglg.spring.service.ScoresService;
import com.dglg.spring.service.TrainingClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class ScoresController {

    @Autowired
    private ScoresService scoresService;
    @Autowired
    private TrainingClassService trainingClassService;

    //插入学生id,并更新后面分数
    @RequestMapping(value = "/importStudentId", method = RequestMethod.POST)
    public Result importStudent_id(@RequestBody Map<String, String> map) {
        Scores scores = new Scores();
        scores.setStudent_id(map.get("student_id"));
        scores.setClass_name(map.get("class_name"));
        System.out.println(scores);
        int count = scoresService.addstudent_id(scores.getStudent_id(), scores.getClass_name());
        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据插入成功！" : "数据插入失败，请重试！";
        return new Result(code, count, msg);
    }

    // 更新个人成绩
    @RequestMapping(value = "/IndividualScoresImport", method = RequestMethod.POST)
    public Result addIndividualscores(@RequestBody Map<String, String> map) {
        Scores scores = new Scores();
        scores.setStudent_id(String.valueOf(map.get("student_id")));
        scores.setAttendance_points(map.get("attendance_points"));
        scores.setGuidance_points(map.get("guidance_points"));
        scores.setClasspeformance_score(map.get("classpeformance_score"));

        scores.Individual_scores = (double) (Integer.parseInt(scores.attendance_points) * 0.3 + Integer.parseInt(scores.guidance_points) * 0.3 + Integer.parseInt(scores.classpeformance_score) *0.4);//个人得分

        int count = scoresService.updateIndividual_scores(scores.getStudent_id(), scores.getIndividual_scores());//更新个人分数
        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据更新成功！" : "数据更新失败，请重试！";

        return new Result(code, count, msg);
    }

    // 更新教师评分
    @RequestMapping(value = "/TeachergroupScoresImport", method = RequestMethod.POST)
    public Result addteachergroupscores(@RequestBody Map<String, String> map) {
        Scores scores = new Scores();
        scores.setStudent_id(String.valueOf(map.get("student_id")));
        scores.setTeachergroup_score_1(map.get("teachergroup_score_1"));
        scores.setTeachergroup_score_2(map.get("teachergroup_score_2"));
        scores.setTeachergroup_score_3(map.get("teachergroup_score_3"));
        scores.setTeachergroup_score_4(map.get("teachergroup_score_4"));

        scores.teachergroup_score =(double)(Integer.parseInt(scores.teachergroup_score_1)* 0.2 + Integer.parseInt(scores.teachergroup_score_2 )* 0.2+
                Integer.parseInt(scores.teachergroup_score_3 )* 0.5+ Integer.parseInt(scores.teachergroup_score_4) * 0.1);//教师小组评分
        System.out.println(scores);
        int count2 = scoresService.update_teachergroup_score(scores.getStudent_id(), scores.getTeachergroup_score());//跟新教师小组分数
        Integer code = count2 != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count2 != 0 ? "数据更新成功！" : "数据更新失败，请重试！";

        return new Result(code, count2, msg);
    }

    // 更新专家评分
    @RequestMapping(value = "/ExpertpanelScoresImport", method = RequestMethod.POST)
    public Result addexpertpanel_rating(@RequestBody Map<String, String> map) {
        int count = 1;
        String className = map.get("className");
        String groupName = map.get("groupName");

        List<String> studentIds = trainingClassService.getStudentIdByCP(className, groupName);
        System.out.println(studentIds);

        for(String s: studentIds) {
            Scores scores = new Scores();
            scores.setStudent_id(s);

            scores.setExpertpanel_rating_1(map.get("expertpanel_rating_1"));
            scores.setExpertpanel_rating_2(map.get("expertpanel_rating_2"));
            scores.setExpertpanel_rating_3(map.get("expertpanel_rating_3"));
            scores.setExpertpanel_rating_4(map.get("expertpanel_rating_4"));
            scores.setExpertpanel_rating_5(map.get("expertpanel_rating_5"));
            scores.setExpertpanel_rating_6(map.get("expertpanel_rating_6"));
            scores.setExpertpanel_rating_7(map.get("expertpanel_rating_7"));
            scores.setExpertpanel_rating_8(map.get("expertpanel_rating_8"));
            System.out.println(scores);

            scores.expertpanel_rating =(double)(Integer.parseInt(scores.expertpanel_rating_1) * 0.1 + Integer.parseInt(scores.expertpanel_rating_2) * 0.1+
                    Integer.parseInt(scores.expertpanel_rating_3 )* 0.1+Integer.parseInt(scores.expertpanel_rating_4)* 0.2 + Integer.parseInt(scores.expertpanel_rating_5) *0.1 + Integer.parseInt(scores.expertpanel_rating_6 )*0.2+
                    Integer.parseInt(scores.expertpanel_rating_7) * 0.1 + Integer.parseInt(scores.expertpanel_rating_8) * 0.1);//专家小组评分

            count = scoresService.updateexpertpanel_rating(scores.getStudent_id(), scores.getExpertpanel_rating());
            count ++;
        }

        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据更新成功！" : "数据更新失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据学号更新小组分数
    @GetMapping(value = "/selectgroup/{student_id}")
    public Result selectgroup(@PathVariable("student_id") String student_id) {
        Scores scores = new Scores();
        Scores scores1 = scoresService.select_group_type(student_id);

        scores.group_scores = scores1.getTeachergroup_score()* 0.5 + scores1.getExpertpanel_rating() * 0.5;//小组得分
        int count = scoresService.updategroup_scores(student_id, scores.getGroup_scores());
        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据更新成功！" : "数据更新失败，请重试！";

        return new Result(code, count, msg);
    }

    // 根据学号更新综合得分
    @GetMapping( value ="/allcomposite/{student_id}")
    public Result allcomposite(@PathVariable("student_id") String student_id){
        Scores scores = new Scores();
        Scores scores2 = scoresService.select_allcomposite_type(student_id);
        scores.composite_score =scores2.getIndividual_scores() * 0.4 + scores2.getGroup_scores() * 0.3 + scores2.getExpertpanel_rating() * 0.3;

        int count = scoresService.updatecomposite_score(student_id,scores.getComposite_score());
        Integer code = count != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = count != 0 ? "数据更新成功！" : "数据更新失败，请重试！";

        return new Result(code, count, msg);
    }

}
